package com.example.project.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

/**
 * @author 邓凯献
 *         create 17-12-8
 *         description: 类属性操作工具类
 */
public class ClazzUtils {
    /**
     * @param source 原数据
     * @param target 目标
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     */
    public static void copy(Object source, Object target) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

        Class sourceClass = source.getClass();//得到对象的Class
        Class targetClass = target.getClass();//得到对象的Class

        Field[] sourceFields = sourceClass.getDeclaredFields();//得到Class对象的所有属性
        Field[] targetFields = targetClass.getDeclaredFields();//得到Class对象的所有属性

        for (Field sourceField : sourceFields) {
            String name = sourceField.getName();//属性名
            Class type = sourceField.getType();//属性类型

            String methodName = name.substring(0, 1).toUpperCase() + name.substring(1);

            Method getMethod = sourceClass.getMethod("get" + methodName);//得到属性对应get方法

            Object value = getMethod.invoke(source);//执行源对象的get方法得到属性值

            if (!AssertUtils.isNotNull(value)) {
                continue;
            }
            for (Field targetField : targetFields) {
                String targetName = targetField.getName();//目标对象的属性名

                if (targetName.equals(name)) {
                    Method setMethod = targetClass.getMethod("set" + methodName, type);//属性对应的set方法

                    setMethod.invoke(target, value);//执行目标对象的set方法
                }
            }
        }
    }



    /**
     *根据属性名获取属性值
     * @param fieldName 字段名称
     * @param o　对象
     * @return  Object
     */
    public static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(o, new Object[] {});
            return value;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     *获取CLASS属性数组
     *@param clazz 类class
     */
    public static Field[] getFileds(Class clazz){
        try {
            return  clazz.getDeclaredFields();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     *获取CLASS属性数组
     * @param clazz 类class
     */
    public static List<Field> getFiledList(Class clazz){
        try {
            return Arrays.asList(clazz.getDeclaredFields());
        } catch (Exception e) {
            return null;
        }
    }





}